﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EmpAwardEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id) && id != "undefined")
                {
                    var v = ctx.EmpAwards.Single(r => r.Id == int.Parse(id));
                    txtDate.Value = v.AwardDate.ToString("yyyy-MM-dd");
                    txtType.Value = v.AwardType;
                    txtItem.Value = v.AwardItem;
                    txtContent.Value = v.AwardContent;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string id = Request.QueryString["id"];
            EmpAward emp;
            if (!string.IsNullOrEmpty(id) && id != "undefined")
            {
                emp = ctx.EmpAwards.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                emp = new EmpAward();
                emp.EmpId = int.Parse(Request.QueryString["empId"]);
                ctx.EmpAwards.InsertOnSubmit(emp);
            }
            emp.AwardDate = DateTime.Parse(txtDate.Value);

            emp.AwardContent = txtContent.Value.Trim();
            emp.AwardItem = txtItem.Value.Trim();
            emp.AwardType = txtType.Value.Trim();
            
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + emp.Id + "';window.close();</script>");
        }
    }
}